package com.huike.review.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.mapper.MybatisReviewMapper;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * mybatis复习使用的业务层
 * 注意该业务层和我们系统的业务无关，主要是让同学们快速熟悉系统的
 */
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private MybatisReviewMapper reviewMapper;

    /**
     * 新增
     *
     * @param name
     * @param age
     * @param sex
     * @return
     */
    @Override
    public int insertReview(String name, Integer age, String sex) {
        MybatisReviewVO vo = new MybatisReviewVO();
        vo.setName(name);
        vo.setAge(age);
        vo.setSex(sex);
        int result = reviewMapper.insertReview(vo);
        return result;
    }

    /**
     * 修改
     *
     * @param mybatisReviewVO
     * @return
     */
    @Override
    public int updateById(MybatisReviewVO mybatisReviewVO) {
        int result = reviewMapper.updateById(mybatisReviewVO);
        return result;
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int result = reviewMapper.deleteById(id);
        return result;
    }

    /**
     * 单个查询
     *
     * @param id
     * @return
     */
    @Override
    public List<Review> getById(Integer id) {
        List<Review> list = reviewMapper.getById(id);
        return list;
    }

    @Override
    public TableDataInfo getDataByPage(Integer pageNum, Integer pageSize) {
        //分页插件 拼接limt语句
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper层方法
        List<Review> list = reviewMapper.getDataByPage();
        //创建TableDataInfo对象
        TableDataInfo tableDataInfo = new TableDataInfo();
        //给rows和total赋值
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(((Page)list).getTotal());
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

}
